<template>
  <el-dialog
    :model-value="modelValue"
    @update:model-value="(value) => emit('update:modelValue', value)"
    :title="isEdit ? '编辑应急预案' : '新增应急预案'"
    width="900px"
    :close-on-click-modal="false"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="120px"
      size="default"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="预案编号" prop="planNo">
            <el-input v-model="form.planNo" placeholder="请输入预案编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预案名称" prop="planName">
            <el-input v-model="form.planName" placeholder="请输入预案名称" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="预案类型" prop="planType">
            <el-select v-model="form.planType" placeholder="选择预案类型" style="width: 100%">
              <el-option label="防洪预案" value="flood_control" />
              <el-option label="抗旱预案" value="drought_control" />
              <el-option label="地震预案" value="earthquake" />
              <el-option label="火灾预案" value="fire" />
              <el-option label="设备故障预案" value="equipment_failure" />
              <el-option label="综合预案" value="comprehensive" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预案级别" prop="planLevel">
            <el-select v-model="form.planLevel" placeholder="选择预案级别" style="width: 100%">
              <el-option label="特别重大(Ⅰ级)" value="level1" />
              <el-option label="重大(Ⅱ级)" value="level2" />
              <el-option label="较大(Ⅲ级)" value="level3" />
              <el-option label="一般(Ⅳ级)" value="level4" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="制定单位" prop="organizationUnit">
            <el-input v-model="form.organizationUnit" placeholder="请输入制定单位" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="制定日期" prop="createDate">
            <el-date-picker
              v-model="form.createDate"
              type="date"
              placeholder="选择制定日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="负责人" prop="responsiblePerson">
            <el-input v-model="form.responsiblePerson" placeholder="请输入负责人" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="适用范围" prop="applicableScope">
            <el-input v-model="form.applicableScope" placeholder="请输入适用范围" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预案状态" prop="planStatus">
            <el-select v-model="form.planStatus" placeholder="选择预案状态" style="width: 100%">
              <el-option label="草案" value="draft" />
              <el-option label="审核中" value="reviewing" />
              <el-option label="已发布" value="published" />
              <el-option label="已废止" value="abolished" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="风险评估" prop="riskAssessment">
            <el-input
              v-model="form.riskAssessment"
              type="textarea"
              :rows="4"
              placeholder="请输入风险评估内容"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="应急目标" prop="emergencyObjective">
            <el-input
              v-model="form.emergencyObjective"
              type="textarea"
              :rows="3"
              placeholder="请输入应急目标"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="组织架构" prop="organizationStructure">
            <el-input
              v-model="form.organizationStructure"
              type="textarea"
              :rows="4"
              placeholder="请输入组织架构说明"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="应急措施" prop="emergencyMeasures">
            <el-input
              v-model="form.emergencyMeasures"
              type="textarea"
              :rows="6"
              placeholder="请输入详细应急措施"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="资源配置" prop="resourceAllocation">
            <el-input
              v-model="form.resourceAllocation"
              type="textarea"
              :rows="4"
              placeholder="请输入资源配置说明"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="通信联络" prop="communicationPlan">
            <el-input
              v-model="form.communicationPlan"
              type="textarea"
              :rows="3"
              placeholder="请输入通信联络方案"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="培训要求" prop="trainingRequirements">
            <el-input
              v-model="form.trainingRequirements"
              type="textarea"
              :rows="3"
              placeholder="请输入培训要求"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="审批人" prop="approver">
            <el-input v-model="form.approver" placeholder="请输入审批人" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审批日期" prop="approvalDate">
            <el-date-picker
              v-model="form.approvalDate"
              type="date"
              placeholder="选择审批日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :rows="3"
              placeholder="请输入备注信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="emit('update:modelValue', false)">取消</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'

interface Props {
  modelValue: boolean
  planData?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  planData: null
})

const emit = defineEmits<Emits>()

const formRef = ref<FormInstance>()
const isEdit = ref(false)

const form = reactive({
  planNo: '',
  planName: '',
  planType: '',
  planLevel: '',
  organizationUnit: '',
  createDate: '',
  responsiblePerson: '',
  contactPhone: '',
  applicableScope: '',
  planStatus: '',
  riskAssessment: '',
  emergencyObjective: '',
  organizationStructure: '',
  emergencyMeasures: '',
  resourceAllocation: '',
  communicationPlan: '',
  trainingRequirements: '',
  approver: '',
  approvalDate: '',
  remark: ''
})

const rules: FormRules = {
  planNo: [
    { required: true, message: '请输入预案编号', trigger: 'blur' }
  ],
  planName: [
    { required: true, message: '请输入预案名称', trigger: 'blur' }
  ],
  planType: [
    { required: true, message: '请选择预案类型', trigger: 'change' }
  ],
  planLevel: [
    { required: true, message: '请选择预案级别', trigger: 'change' }
  ],
  organizationUnit: [
    { required: true, message: '请输入制定单位', trigger: 'blur' }
  ],
  createDate: [
    { required: true, message: '请选择制定日期', trigger: 'change' }
  ],
  responsiblePerson: [
    { required: true, message: '请输入负责人', trigger: 'blur' }
  ],
  contactPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  emergencyMeasures: [
    { required: true, message: '请输入应急措施', trigger: 'blur' }
  ]
}

// 重置表单
const resetForm = () => {
  Object.assign(form, {
    planNo: '',
    planName: '',
    planType: '',
    planLevel: '',
    organizationUnit: '',
    createDate: '',
    responsiblePerson: '',
    contactPhone: '',
    applicableScope: '',
    planStatus: '',
    riskAssessment: '',
    emergencyObjective: '',
    organizationStructure: '',
    emergencyMeasures: '',
    resourceAllocation: '',
    communicationPlan: '',
    trainingRequirements: '',
    approver: '',
    approvalDate: '',
    remark: ''
  })
  formRef.value?.clearValidate()
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    
    // 这里应该调用API保存数据
    console.log('提交表单数据:', form)
    
    ElMessage.success(isEdit.value ? '修改成功' : '添加成功')
    emit('success')
    emit('update:modelValue', false)
  } catch (error) {
    ElMessage.error('请检查表单填写是否正确')
  }
}

// 监听弹窗打开，初始化表单数据
watch(() => props.modelValue, (newVal) => {
  if (newVal) {
    if (props.planData) {
      isEdit.value = true
      Object.assign(form, props.planData)
    } else {
      isEdit.value = false
      resetForm()
    }
  }
}, { immediate: true })
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>